package com.wsc.wisdomchina.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.ACache;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.fourmodule.HelpOldManDetailActivity;
import com.wsc.wisdomchina.activitity.samemodule.ActivityDetailActivity;
import com.wsc.wisdomchina.activitity.samemodule.InfoModuleDetailActivity;
import com.wsc.wisdomchina.adapter.ActivityAdapter;
import com.wsc.wisdomchina.adapter.AroundFindAdapter;
import com.wsc.wisdomchina.adapter.InfoListAdapter;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.javaBean.ActivityListBean;
import com.wsc.wisdomchina.javaBean.AroundFindListBean;
import com.wsc.wisdomchina.javaBean.InfoListBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.RespondObj;
import com.wsc.wisdomchina.javaBean.wisdomParty.InfoAndActivityChildrenBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.List;

/**
 * @author jaily.zhang
 * @version V1.3.1
 * @Description 选中的行业标签
 * @date 2015-4-29 下午4:47:24
 */
public class ChildrenModuleFragment extends Fragment implements ContributorView {
    private PullToRefreshListView lv_today_information;
    private ListView actualListView;
    private final String EightModuleFragment = ChildrenModuleFragment.class.getSimpleName();

    private String cid;
    private String id;
    private Context mcontext;
    private int skip = 0;
    private final int limit = 6;
    private ContributorPresenter contributorPresenter;
    private boolean isfresh = true;
    private ProgressBar pb_todayInfo;
    private MyApplication myApplication;
//    private ACache aCache;
//    private String aCacheFlagKey;
    private InfoAndActivityChildrenBean infoAndActivityChildrenBean;
    private final String INFOLISTFLAG="INFOLISTFLAG";
    private final String ACTIVITYLISTFLAG="ACTIVITYLISTFLAG";
    private List<InfoListBean> infoListBeanList;
    private List<ActivityListBean> activityListBeanList;
    private InfoListAdapter infoListAdapter;
    private ActivityAdapter activityAdapter;

    public static ChildrenModuleFragment newInstance(Bundle b) {
        ChildrenModuleFragment selectedOneFragment = new ChildrenModuleFragment();
        selectedOneFragment.setArguments(b);
        return selectedOneFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        cid = getArguments().getString("cid");
        id=getArguments().getString("id");
        infoAndActivityChildrenBean=(InfoAndActivityChildrenBean) getArguments().getSerializable("obj");
//        aCacheFlagKey=EightModuleFragment+id;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.hotnewsfragment_layout, container, false);
        mcontext = getActivity();
//        aCache=ACache.get(mcontext);
        myApplication=(MyApplication) getActivity().getApplication();
        contributorPresenter = new ContributorPresenter(this, mcontext);
        initView(view);
        return view;
    }

    /**
     * @param view
     * @Description 初始化View控件
     * @author jaily.zhang
     */
    private void initView(View view) {

        lv_today_information = (PullToRefreshListView) view.findViewById(R.id.lv_today_information);
        lv_today_information.setMode(Mode.BOTH);
        pb_todayInfo = (ProgressBar) view.findViewById(R.id.pb_todayInfo);
        actualListView = lv_today_information.getRefreshableView();

        actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        actualListView.setDivider(null);
        Tools.showRefreshView(lv_today_information);

        lv_today_information.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // TODO Auto-generated method stub
                // Update the LastUpdatedLabe
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(Mode.PULL_FROM_START)) {
                    isfresh = true;
                    Tools.showRefreshView(lv_today_information);
                    skip = 0;
                    showData(skip);
                } else {
                    isfresh = false;
                    Tools.showRefreshView(lv_today_information);
                    skip += limit;
                    showData(skip);
                }

            }
        });

        actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if(infoAndActivityChildrenBean.getTempletType().equals("0"))
                {
                    //点击进入资讯详情
                    Intent intent = new Intent(mcontext, InfoModuleDetailActivity.class);
                    intent.putExtra("id", infoListBeanList.get(position-1).getInformationId());
                    intent.putExtra("name",infoListBeanList.get(position-1).getShowColumn1());
                    startActivity(intent);
                }else {
                    //点击进入活动详情
                    Intent intent = new Intent(mcontext, ActivityDetailActivity.class);
                    intent.putExtra("bid", activityListBeanList.get(position-1).getId());
                    intent.putExtra("cid",cid);
                    intent.putExtra("type",0);
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * @Description 服务器获取数据
     * @author jaily.zhang
     */
    private void showData(int skip) {
//        if(aCache.getAsString(aCacheFlagKey)!=null)
//        {
//            pb_todayInfo.setVisibility(View.GONE);
//            List<AroundFindListBean> homeNewsItem = JsonTools.jsonStringToList(aCache.getAsString(aCacheFlagKey), AroundFindListBean.class);
//            if (homeNewsItem != null && homeNewsItem.size() > 0) {
//                homeNewsList = homeNewsItem;
//                quanBuAdapter = new AroundFindAdapter(mcontext, homeNewsList);
//                actualListView.setAdapter(quanBuAdapter);
//            }
//        }else
//        {
            //首页资讯
        if(infoAndActivityChildrenBean.getTempletType().equals("0"))
        {
            //资讯
            contributorPresenter.getInformationList(cid,id,skip,limit,INFOLISTFLAG);
        }else {
            //活动
            contributorPresenter.getActivityList(cid,id,skip,limit,ACTIVITYLISTFLAG);
        }
//        }

    }


    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //资讯
            if (flag.equals(INFOLISTFLAG)) {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<InfoListBean> floorActivityListBeen = JsonTools.jsonStringToList(jsonMessage, InfoListBean.class);
                    if (floorActivityListBeen != null && floorActivityListBeen.size() > 0) {
                        if (isfresh) {
                            infoListBeanList = floorActivityListBeen;
                            infoListAdapter = new InfoListAdapter(mcontext, infoListBeanList);
                            actualListView.setAdapter(infoListAdapter);
                        } else {
                            infoListBeanList.addAll(floorActivityListBeen);
                            infoListAdapter.notifyDataSetChanged();
                        }

                    }else
                    {
                    }

                }
            }
            //活动
            else if(flag.equals(ACTIVITYLISTFLAG))
            {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<ActivityListBean> floorActivityListBeen = JsonTools.jsonStringToList(jsonMessage, ActivityListBean.class);
                    if (floorActivityListBeen != null && floorActivityListBeen.size() > 0) {
                        if (isfresh) {
                            activityListBeanList = floorActivityListBeen;
                            activityAdapter = new ActivityAdapter(mcontext, activityListBeanList);
                            actualListView.setAdapter(activityAdapter);
                        } else {
                            activityListBeanList.addAll(floorActivityListBeen);
                            activityAdapter.notifyDataSetChanged();
                        }

                    }else
                    {
                    }

                }
            }
        }catch (Exception e)
        {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
        lv_today_information.onRefreshComplete();
        pb_todayInfo.setVisibility(View.GONE);
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }


    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (myApplication.getInformationFragmentSelected() != null) {
            if (myApplication.getInformationFragmentSelected().equals(EightModuleFragment + id)) {
                isfresh = true;
                skip = 0;
                showData(skip);
            }
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}
